<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head th:include="~{inc/fragments :: header}">
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col">
            <h1>登录</h1>
            <p>
                <a href="/">返回首页</a>
                <a href="/register">注册</a>
            </p>
        </div>
    </div>
    <div class="row">
        <div class="col-6">
            <form onsubmit="doLogin();return false;">
                <div class="form-group">
                    <label for="username">用户名</label>
                    <input type="text" class="form-control" id="username">
                </div>
                <div class="form-group">
                    <label for="password">密码</label>
                    <input type="password" class="form-control" id="password">
                </div>
                <button type="submit" class="btn btn-primary">登录</button>
            </form>
        </div>
    </div>
</div>

<div th:replace="~{inc/fragments :: js}"></div>
<script>
    function doLogin() {
        var data2 = {username: $('#username').val(), password: $('#password').val()};
        $.ajax({
            url: '/api/user/login',
            method: 'POST',
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify(data2),
            success: function (resp) {
                console.info(resp);
                if (resp.code === 200) {
                    window.location.href = '/';
                } else {
                    alert(resp.message);
                }
            },
            error: function () {
            }
        });
    }
</script>
</body>
</html>